'use strict'
class db_uploadElife{

    constructor (mdb) {
        this.db = mdb
    }
    

    //创建步骤的编号，示例：taobao_fun1、taobao_fun2
    async makeEname(data){

        let sqlInsertFun = `select ename from appfeatures where appname='${data.appname}';`
        let r = await this.db.query(sqlInsertFun);
        let arr = r.rows;
        if(arr.length === 0){
            return `${data.appname}_fun1`
        }
        let numArr = arr.map((item)=>{
            return parseInt(item.ename.slice(item.ename.length-1))
        })
        let row = Math.max.apply(null,numArr)
        return `${data.appname}_fun${row+1}`
    }

    async getAppId(appname){

        let getAppIdSql = `select id from app where appname='${appname}';`
        let r = await this.db.query(getAppIdSql);
        let id = r.rows[0].id;
        return id;
    }


    //向app表里添加数据，新增一个app
    async insertApp(data){
        
        let sql =  'INSERT INTO app(appname,image,cname) VALUES ($1, $2, $3)'
        let ret = await this.db.query(sql, [
            data.appname, data.image, data.cname
        ])
        if (ret.rowCount <= 0) {
            return false
          }
    }


    //向appfeatures表里添加数据，新增一个app的功能
    async insertAppFunc(data){

        let appId = await this.getAppId(data.appname)
        
        let ename = await this.makeEname(data)
       
        let sql =  'INSERT INTO appfeatures(name,appname,ename,icon,appid) VALUES ($1, $2, $3, $4, $5)'
        let ret = await this.db.query(sql, [
            data.name,
            data.appname,
            ename,
            data.iconname,
            appId
        ])
        if (ret.rowCount <= 0) {
            return false
          }
    }


    //增加功能时，建立一个新表，示例：taobao_fun4
    async createFuncTable(data){

        let ename = await this.makeEname(data)

        let ename1 = ename.slice(0,ename.length-1)
        let ename2 = parseInt(ename.slice(ename.length-1))-1
        ename = `${ename1}${ename2}`
        let sql =  `create table ${ename}(id serial primary key, image varchar(20), text varchar(50), dote1 smallint, dote2 smallint, dote3 smallint, dote4 smallint)`
        let ret = await this.db.query(sql)
    }


    //增加步骤，向taobao_fun4表里增加一行
    async insertAppStep(data){

        let sql = `insert into ${data.table}(image,text,dote1,dote2,dote3,dote4) values ($1, $2, $3,$4,$5, $6)`
        let ret = await this.db.query(sql, [
            data.image, data.text, data.dote1, data.dote2, data.dote3, data.dote4
        ])
        if (ret.rowCount <= 0) {
            return false
        }

    }


    //增加文章，向passage表里增加文章
    async addpassage(data){
        let date = new Date().toLocaleDateString()
        let sql = 'INSERT INTO passage(title,image,content,voice,date) VALUES ($1,$2,$3,$4,$5)'
        let ret = await this.db.query(sql,[
            data.title,data.image,data.content,data.voice,date
        ])
        if (ret.rowCount <= 0) {
            return false
        }
        return date
    }





    //微信用户上传app名字和功能名字加图标
    async wxUserUploadFunc(data){

        // let data = {
        //     openid:c.box.user.openid,
        //     appname:c.body.appname,
        //     funcname:c.body.funcname,
        //     imagename:imageName
        // }
        console.log(444,data);
        let time = new Date().getTime()
        let sql = `INSERT INTO reviewing(appname,funcname,icon,openid,review,time) VALUES ('${data.appname}','${data.funcname}','${data.imagename}','${data.openid}',0,'${time}') returning id`
        let r = await this.db.query(sql)
        console.log(r.rows[0]);
        let id = r.rows[0].id;
        return id;
    }


    //微信用户上传app步骤
    async wxUserUploadFunc(data){

        // let data = {
        //     funcid:c.body.funcid,
        //     stepid:c.body.stepid,
        //     dote1:c.body.dote1,
        //     dote2:c.body.dote2,
        //     text:c.body.text,
        //     imagename:fname
        // }

        let dote3,dote4;

        if(data.dote1 < 30 || data.dote2 < 30){
            dote3 = data.dote1 + 15;
            dote4 = data.dote2 + 15;
        }else{
            dote3 = data.dote1 - 15;
            dote4 = data.dote2 - 15;
        }


        let sql = 'INSERT INTO usercreateappfunc (funcid,stepid,dote1,dote2,dote3,dote4,text,img) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)'
        let ret = await this.db.query(sql,[
            data.funcid,
            data.stepid,
            data.dote1,
            data.dote2,
            dote3,
            dote4,
            data.text,
            data.imagename
        ])
        if (ret.rowCount <= 0) {
            return false
        }
        return true;
    }
}

module.exports = db_uploadElife